package org.kardigen.lg.admin.client.training;

import java.util.ArrayList;
import java.util.List;

import org.kardigen.lg.service.client.proxy.TrainingChapterProxy;
import org.kardigen.lg.service.client.proxy.TrainingProxy;
import org.kardigen.lg.service.client.proxy.WordImageProxy;
import org.kardigen.lg.service.client.proxy.WordProxy;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestContext;
import org.kardigen.lg.service.client.requestfactory.TrainingRequestFactory;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.user.client.Window;

public class WordEditorViewPresenter implements WordEditorViewDisplay.Presenter {

	private final WordEditorViewDisplay display;
	private EventBus eventBus;
	private TrainingRequestFactory requestFactory;
	private TrainingRequestContext requestContext;
	private WordProxy editableProxy;
	private TrainingChapterProxy editableParent;
	private TrainingChapterProxy parent;
	private WordProxy proxy;
	private TrainingProxy root;

	public WordEditorViewPresenter(WordEditorViewDisplay display) {
		this.display = display;
		display.setPresenter(this);

	}

	public void onSave() {
		String value = display.getWord().getValue();
		if (value != null && !value.trim().isEmpty()) {

			final List<WordProxy> chapters = editableParent.getWords();
			int idx = 0;
			for (; idx < chapters.size(); idx++) {
				if (editableProxy.equals(chapters.get(idx))) {
					break;
				}
			}

			final int index = idx;

			editableProxy.setWord(value);

			WordImageProxy image = editableProxy.getImage();
			if (image == null) {
				image = requestContext.create(WordImageProxy.class);
				editableProxy.setImage(image);
			}

			image.setImageUrl(display.getImageUrl().getValue());

			requestContext.saveTraining(root).with("word", "word.image").fire(new Receiver<TrainingProxy>() {

				      @Override
				public void onSuccess(TrainingProxy response) {
					      if (response == null) {
						      Window.alert("Cannot save chapter!");
					      } else {
						      Window.alert("Chapter saved!");

						// parent = response;
						// proxy = response.getWords().get(index);
						//
						// requestContext =
						// requestFactory.createTrainingRequestContext();
						// editableParent = requestContext.edit(response);
						// editableProxy = requestContext.edit(chapters.get(index));
					      }
				      }
			      });
		}
	}

	public void start(TrainingProxy root, TrainingChapterProxy parent, WordProxy proxy, EventBus eventBus) {
		this.root = root;
		this.parent = parent;
		this.proxy = proxy;
		this.eventBus = eventBus;
		initializeRequestFactory();
		requestContext = requestFactory.createTrainingRequestContext();

		root = requestContext.edit(root);

		display.getWord().setValue("");

		editableParent = requestContext.edit(parent);
		List<WordProxy> words = editableParent.getWords();
		if (words == null) {
			words = new ArrayList<WordProxy>();
			editableParent.setWords(words);
		}

		if (proxy == null) {
			editableProxy = requestContext.create(WordProxy.class);
			words.add(editableProxy);
			display.getLabel().setText("Creating new word");
		} else {
			int idx = parent.getWords().indexOf(proxy);
			editableProxy = requestContext.edit(words.get(idx));
			display.getLabel()
			      .setText("Editing word: " + proxy.getWord() + " with URL: " + proxy.getImage().getImageUrl());
		}
	}

	private void initializeRequestFactory() {
		if (requestFactory == null) {
			requestFactory = GWT.create(TrainingRequestFactory.class);
			requestFactory.initialize(eventBus);
		}
	}

}
